<template>
  <div>
    <div>
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
        <!-- <el-checkbox label="复选框 B" />
        <el-checkbox label="复选框 C" />
        <el-checkbox label="禁用" disabled />
        <el-checkbox label="选中且禁用" disabled /> -->
      </el-checkbox-group>
    </div>
    <div style="margin-top:20px">
      <el-button type="primary" @click="onSubmit">更改权限</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>

  </div>

</template>
<script>
import { getRolesListRequest } from '@/api/setting'
import { userInfoDetailRequest, rolePromissiondRequest } from '@/api/user'
export default {
  name: '',
  props: {
    currentId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      // console.log(this.currentId)
      try {
        const res = await getRolesListRequest({
          page: 1,
          pagesize: 100
        })
        console.log('getRolesListRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          // console.log(res.data.rows)
          this.list = res.data.rows
          // console.log(this.list)
        } else {
          this.$message.success(res.message)
        }
        const info = await userInfoDetailRequest(this.currentId)
        console.log('userInfoDetailRequest: ', info)
        if (info.success) {
          this.$message.success(info.message)
          this.checkList = info.data.roleIds || []
          // console.log(this.checkList)
        } else {
          this.$message.success(info.message)
        }
      } catch (err) {
        console.log('userInfoDetailRequest: ', err)
      }
    },
    // 更新用户权限
    // async updateRoleById() {
    //   try {
    //     const res = await updateRoleByIdRequest(this.currentId)
    //     console.log('updateRoleByIdRequest: ', res)
    //     if (res.success) {
    //       this.$message.success(res.message)
    //       this.cancel()
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (err) {
    //     console.log('updateRoleByIdRequest: ', err)
    //   }
    // },
    // 分配权限
    async rolePromission() {
      // console.log(this.checkList)
      try {
        const res = await rolePromissiondRequest({
          id: this.currentId,
          roleIds: this.checkList
        })
        console.log('rolePromissiondRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.cancel()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('rolePromissiondRequest: ', err)
      }
    },
    onSubmit() {
      this.rolePromission()
    },
    cancel() {
      this.$emit('close')
      // this.checkList = []
    }
  }

}
</script>
